<template>
    <el-form label-width="100px" :rules="rules" ref="formRef" :model="form">
        <el-form-item label="公司名称" prop="companyName">
            <el-input v-model="form.companyName" aria-placeholder="请输入公司名称"></el-input>
        </el-form-item>
        <el-form-item label="公司地址" prop="address">
            <el-input v-model="form.address" type="textarea" aria-placeholder="请输入公司地址"></el-input>
        </el-form-item>
        <el-form-item label="公司电话" prop="phone">
            <el-input v-model="form.phone" aria-placeholder="请输入公司电话"></el-input>
        </el-form-item>
        <el-form-item label="公司邮件" prop="email">
            <el-input v-model="form.email" aria-placeholder="请输入公司邮件"></el-input>
        </el-form-item>
        <el-form-item label="公司简介" prop="introduction">
            <el-input v-model="form.introduction" type="textarea" :rows="4" aria-placeholder="请输入公司简介"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="submitForm">提交</el-button>
            <el-button @click="resetForm">重置</el-button>
        </el-form-item>
    </el-form>
</template>
<script >
import {ref} from 'vue';
import {ElMessage} from 'element-plus';
import axios from 'axios';

export default{
    setup(){
        const form = ref({
            companyName:'',
            address:'',
            phone:'',
            email:'',
            introduction:''
        });

        const rules = {
            companyName:[
                {required: true,message:'请输入公司名称' , trigger:'blur'}
            ],
            address:[
                {required: true,message:'请输入公司地址' , trigger:'blur'}
            ], 
            phone:[
                {required: true,message:'请输入公司手机' , trigger:'blur'}
            ], 
            email:[
                {required: true,message:'请输入公司邮箱' , trigger:'blur'}
            ], 
            introduction:[
                {required: true,message:'请输入公司简介' , trigger:'blur'}
            ]
        };

        const formRef = ref(null);


        const submitForm = () =>{
            formRef.value.validate(async (valid) =>{
                
                if(valid){
                    await axios.post("api/company/add",form.value)
                    ElMessage.success('提交成功!');
                    console.log('表单数据：',form.value);
                    
                }else{
                    ElMessage.error('请检查输入内容')
                }
            });
        };


        const resetForm = () => {
            formRef.value.resetFields();
        };

        return {
            form,
            rules,
            formRef,
            submitForm,
            resetForm
        };
    }
};
</script>
<style lang="less" scoped>
.el-form{
    max-width: 500px;
    margin: 0 auto;
}
</style>
